﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Shapes;
using System.Windows.Media;

namespace Search
{
    class SearchViewer : StackPanel
    {
        public const int WIDTH = 200;

        private TextBox searchText = null;
        private ListBox resultsList = null;
        private ISearchEngine mySearch = new BasicSearchEngine();

        public SearchViewer(int height)
        {
            searchText = new TextBox();
            searchText.Width = WIDTH;
            this.Children.Add(searchText);
               
            resultsList = new ListBox();
            resultsList.Height = height;
            resultsList.MaxWidth = WIDTH;
            resultsList.MaxHeight = height;
            resultsList.MouseRightButtonDown += new MouseButtonEventHandler(
                                          resultsList_MouseRightButtonDown);
            this.Children.Add(resultsList);
           
            searchText.TextChanged += new TextChangedEventHandler(searchText_TextChanged);
        }

        void resultsList_MouseRightButtonDown(object sender, MouseEventArgs e)
        {
            String name = (String)resultsList.SelectedItem;
            //
            // Set root node to name.
            //
        }

        void searchText_TextChanged(object sender, TextChangedEventArgs e)
        {
            resultsList.Items.Clear();

            if (searchText.Text.Length == 0)
                return;

            mySearch.Search(searchText.Text);

            if (mySearch.IsPartialMatchFound)
            {
                resultsList.Items.Add("Did you mean: " + mySearch.GetPartialMatch());
            }

            foreach (String s in mySearch)
            {
                resultsList.Items.Add(s);
            }
        }
    }
}
